package cn.ecnu.controller.resume;

import cn.ecnu.common.Constant;
import cn.ecnu.common.utils.R;
import cn.ecnu.common.validated.InsertGroup;
import cn.ecnu.common.validated.UpdateGroup;
import cn.ecnu.pojo.resume.*;
import cn.ecnu.service.resume.ResumeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Validated
@RestController
@RequestMapping("/resume")
public class ResumeController {

    @Resource
    ResumeService resumeService;

    @PostMapping("/basic")
    public R updateOrInsertResumeBasic(@Validated @RequestBody ResumeBasic resumeBasic){
        resumeService.updateOrInsertResumeBasic(resumeBasic);
        return R.ok();
    }

    @GetMapping("/basic")
    public R getResumeBasic(@RequestParam("userId") Integer userId){
        ResumeBasic result = resumeService.getResumeBasicByUserId(userId);
        if (result == null){
            return R.error(Constant.INTERNAL_ERROR, "基本信息查询错误");
        }
        return R.data(result);
    }



    @PutMapping("/edu")
    public R updateResumeEdu(@Validated(UpdateGroup.class) @RequestBody ResumeEdu resumeEdu){
        if (resumeService.updateResumeEdu(resumeEdu))
            return R.ok();
        return R.error(Constant.INTERNAL_ERROR, "教育经历更新错误");
    }

    @PostMapping("/edu")
    public R insertResumeEdu(@Validated(InsertGroup.class) @RequestBody ResumeEdu resumeEdu){
        if (resumeService.insertResumeEdu(resumeEdu)) {
            return R.ok();
        }
        return R.error(Constant.INTERNAL_ERROR,"教育经历添加错误");
    }

    @GetMapping("/edu")
    public R getResumeEdu(@RequestParam Integer userId){
        List<ResumeEdu> result = resumeService.getResumeEduByUserId(userId);
        if (result == null){
            return R.error(Constant.INTERNAL_ERROR,"教育经历结果查询为空");
        }
        return R.data(result);
    }



    @PutMapping("/extra")
    public R updateResumeExtra(@Validated @RequestBody ResumeExtra resumeExtra){
        if (resumeService.updateResumeExtra(resumeExtra)) {
            return R.ok();
        }
        return R.error(Constant.INTERNAL_ERROR, "在校经历更新错误");
    }

    @PostMapping("/extra")
    public R insertResumeExtra(@RequestBody ResumeExtra resumeExtra){
        if (resumeService.insertResumeExtra(resumeExtra)) {
            return R.ok();
        }
        return R.error(Constant.INTERNAL_ERROR,"在校经历添加错误");
    }

    @GetMapping("/extra")
    public R getResumeExtra(@RequestParam Integer userId){
        List<ResumeExtra> result = resumeService.getResumeExtraByUserId(userId);
        if (result == null){
            return R.error(Constant.INTERNAL_ERROR,"教育经历结果查询为空");
        }
        return R.data(result);
    }



    @PutMapping("/job")
    public R updateResumeJob(@Validated @RequestBody ResumeJob resumeJob){
        if (resumeService.updateResumeJob(resumeJob)) {
            return R.ok();
        }
        return R.error(Constant.INTERNAL_ERROR, "工作经历更新错误");
    }

    @PostMapping("/job")
    public R insertResumeJob(@RequestBody ResumeJob resumeJob){
        if (resumeService.insertResumeJob(resumeJob)) {
            return R.ok();
        }
        return R.error(Constant.INTERNAL_ERROR, "工作经历添加错误");
    }

    @GetMapping("/job")
    public R getResumeJob(@RequestParam("userId") Integer userId){
        List<ResumeJob> result = resumeService.getResumeJobByUserId(userId);
        if (result == null){
            return R.error(Constant.INTERNAL_ERROR,"工作经历结果查询为空");
        }
        return R.data(result);
    }



    @PutMapping("/skill")
    public R updateResumeSkill(@Validated @RequestBody ResumeSkill resumeSkill){
        if (resumeService.updateResumeSkill(resumeSkill)) {
            return R.ok();
        }
        return R.error(Constant.INTERNAL_ERROR, "职业技能更新错误");
    }

    @PostMapping("/skill")
    public R insertResumeSkill(@RequestBody ResumeSkill resumeSkill){
        if (resumeService.insertResumeSkill(resumeSkill)) {
            return R.ok();
        }
        return R.error(Constant.INTERNAL_ERROR,"职业技能添加错误");
    }

    @GetMapping("/skill")
    public R getResumeSkill(@RequestParam("userId") Integer userId){
        List<ResumeSkill> result = resumeService.getResumeSkillByUserId(userId);
        if (result == null){
            return R.error(Constant.INTERNAL_ERROR,"职业技能结果查询为空");
        }
        return R.data(result);
    }



    @PutMapping("/exp")
    public R updateResumeExp(@Validated @RequestBody ResumeExp resumeExp){
        if (resumeService.updateResumeExp(resumeExp)) {
            return R.ok();
        }
        return R.error(Constant.INTERNAL_ERROR, "期望岗位更新错误");
    }

    @PostMapping("/exp")
    public R insertResumeExp(@RequestBody ResumeExp resumeExp){
        if (resumeService.insertResumeExp(resumeExp)) {
            return R.ok();
        }
        return R.error(Constant.INTERNAL_ERROR, "期望岗位添加错误");
    }

    @GetMapping("/exp")
    public R getResumeExp(@RequestParam("userId") Integer userId){
        List<ResumeExp> result = resumeService.getResumeExpByUserId(userId);
        if (result == null){
            return R.error(Constant.INTERNAL_ERROR,"教育经历结果查询为空");
        }
        return R.data(result);
    }
}
